SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[spAntiguedadTodos]
@Fdesde datetime,
@FHasta datetime
AS
SET NOCOUNT ON
delete from tb_Antiguedades where ConceptoAntiguedad = 5

declare @idA int
declare @ad int
Select @ad = 0

-- Saco todos los agentes
declare agentes cursor for Select idAgente from tb_Agentes
open agentes

fetch next from agentes into @ida

while @@fetch_status = 0
begin
	select @ad = 0
	exec spantiguedadAgente @ida, @fdesde, @fhasta, @ad output
	insert into tb_Antiguedades (idAgente, ConceptoAntiguedad, Parametro, FechaDesde, FechaHasta, TotalDias, Anios, Meses, Dias, Manual,
	Recalcular, FechaCalculo)
	values(@ida,5,null,@fdesde,@fhasta,@ad,round(@ad/365,0), round(@ad/30,0),@ad,'N' ,'N' ,getdate())
	fetch next from agentes into @ida
end

close agentes
deallocate agentes
GO
